package com.hwy.widget.suck_top

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hwy.widget.R
import com.hwy.widget.utils.DisplayUtil

/**
 * RecyclerView 分组吸顶效果
 */
class SuckTopActivity : AppCompatActivity() {

    private val dataList = arrayOf(
        0, 1, 2, 3,
        4, 5, 6, 7,
        8, 9, 10, 11,
        12, 13, 14, 15,
        16, 17, 18, 19,
        20, 21, 22, 23, 24,
        25, 26, 27, 28,
        29
    )

    private var recyclerView: RecyclerView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_suck_top)

        initRecycler()

    }

    private fun initRecycler() {
        recyclerView = findViewById<RecyclerView>(R.id.recycler_view)?.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = SuckTopAdapter(context)
            addItemDecoration(
                SuckTopDecoration(
                    adapter as SuckTopAdapter,
                    context,
                    DisplayUtil.dpToPx(30),
                    DisplayUtil.dpToPx(1)
                )
            )
        }
    }

    inner class SuckTopAdapter(val context: Context) :
        RecyclerView.Adapter<RecyclerView.ViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            return SuckTopViewHolder(
                this,
                LayoutInflater.from(context)
                    .inflate(R.layout.layout_suck_top_adapter, parent, false)
            )
        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            (holder as? SuckTopViewHolder)?.bindView(
                position,
                !isUseDecorationGroupName() && isGroupHeader(position)
            )
        }

        override fun getItemCount(): Int {
            return dataList.size
        }

        fun isGroupHeader(position: Int): Boolean {
            return position % 4 == 0
        }

        fun getGroupName(position: Int): String {
            return String.format("分组%d号", (position / 4) + 1)
        }

        fun isUseDecorationGroupName(): Boolean {
            return false
        }

    }

    private inner class SuckTopViewHolder(val adapter: SuckTopAdapter, itemView: View) :
        RecyclerView.ViewHolder(itemView) {
        fun bindView(position: Int, isGroupHeader: Boolean) {
            itemView.findViewById<TextView>(R.id.tv_item)?.run {
                text = "position = $position"
            }

            itemView.findViewById<TextView>(R.id.tv_group_name)?.run {
                isVisible = isGroupHeader
                text = adapter.getGroupName(position)
            }
        }
    }

    private inner class SuckTopDecoration(
        val adapter: SuckTopAdapter,
        context: Context,
        headerHeight: Int,
        decorationHeight: Int
    ) :
        AbstractSuckTopDecoration(context, headerHeight, decorationHeight) {

        private val paint by lazy {
            Paint().apply {
                isAntiAlias = true
                isDither = true
            }
        }

        override fun isOnlyDrawSuckTop(): Boolean {
            return !adapter.isUseDecorationGroupName()
        }

        override fun isGroupHeader(position: Int): Boolean {
            return adapter.isGroupHeader(position)
        }

        override fun drawDecoration(
            boundsRect: Rect,
            canvas: Canvas,
            parent: RecyclerView,
            position: Int
        ) {
            paint.color = Color.parseColor("#66FF00")
            canvas.drawRect(boundsRect, paint)
        }

        override fun drawGroupHeader(
            boundsRect: Rect,
            canvas: Canvas,
            parent: RecyclerView,
            position: Int,
            isSuckTopHeader: Boolean
        ) {
            // 绘制 header 区域
            paint.color = Color.parseColor("#FF6600")
            canvas.drawRect(boundsRect, paint)

            // 绘制分组名称
            val groupName = adapter.getGroupName(position)
            paint.textSize = DisplayUtil.dpToPx(14).toFloat()
            paint.color = context.resources.getColor(R.color.black)
            val startX = DisplayUtil.dpToPx(20).toFloat()
            var baseline = (paint.fontMetrics.descent + paint.fontMetrics.ascent) / 2
            canvas.drawText(
                groupName, startX, if (isSuckTopHeader) {
                    boundsRect.top + boundsRect.bottom - headerHeight / 2 - baseline
                } else {
                    boundsRect.top + headerHeight / 2 - baseline
                }, paint
            )
        }

    }

}